'''
  从tushare获取数据
'''

import tushare as ts
from jqdatasdk import *
from utils import *
import time
import pandas as pd
import traceback
import os

# cost 2k money
token = '09ee3acfffa90ac13830531d9876c1044196a513db0cee10153934d9'

ts.set_token(token)

# 获取每只股日线复权数据
def get_fq_daily():
    pro = ts.pro_api(token)

    # 获取所有股票信息
    data = pro.stock_basic(exchange='', list_status='L', fields='ts_code,symbol,name,area,industry,list_date')
    data.to_csv('ts_stock_basic.csv', index=None)
    for index, row in data.iterrows():

        # if '退' in row['name'] or 'ST' in row['name']:
        #     print(row['ts_code'], row['name'])
        #     continue


        stockid = row['symbol']

        print(index, stockid, row['name'])
        # 前复权
        df = ts.pro_bar(ts_code=row['ts_code'], adj='qfq')
        if df is None:
            continue
        # 后复权
        #df = ts.pro_bar(ts_code=row['ts_code'], adj='hfq')
        #df = pro.daily(ts_code=row['ts_code'])
        df.to_csv('tsdataqfq/' + stockid + '.csv')

# 获取基本面数据
def ts_basic():
    pro = ts.pro_api(token)
    # 获取所有股票信息
    data = pro.stock_basic(exchange='', list_status='L', fields='ts_code,symbol,name,area,industry,list_date')
    for index, row in data.iterrows():
        stockid = row['symbol']
        print(index, stockid, row['name'])
        # 单独获取一只票的日线信息
        df = pro.daily_basic(ts_code=row['ts_code'])
        df.to_csv('tsbasic/' + str(stockid) + '.csv')

if __name__ == '__main__':
    get_fq_daily()
    ts_basic()
